/*
 * @Description: 注入变量
 * @Autor: HuiSir<273250950@qq.com>
 * @Date: 2024-02-06 15:08:36
 * @LastEditTime: 2024-05-31 09:47:01
 */
import dev from "./inject.dev.js";
import pro from "./inject.pro.js";

export default (template) => {
  return replace(template);
};

/**
 * 内容字符替换
 */
function replace(template) {
  const isDEV = process.env.NODE_ENV === "development";
  const pattern = /<inject\s+(\w+)\s*\/>/g;
  let res = template.replace(pattern, (_word, $1) => {
    return isDEV ? dev[$1] : pro[$1];
  });
  // 递归，保证替换完全
  if (pattern.test(res)) {
    res = replace(res);
  }
  return res;
}
